package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.factorydata.mapper.MaterialUnitDataMapper;
import com.ruoyi.factorydata.domain.MaterialUnitData;
import com.ruoyi.factorydata.service.IMaterialUnitDataService;

/**
 * 物料单位Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-07
 */
@Service
public class MaterialUnitDataServiceImpl extends ServiceImpl<MaterialUnitDataMapper, MaterialUnitData> implements IMaterialUnitDataService {
    @Autowired
    private MaterialUnitDataMapper materialUnitDataMapper;

    /**
     * 查询物料单位
     *
     * @param id 物料单位主键
     * @return 物料单位
     */
    @Override
    public MaterialUnitData selectMaterialUnitDataById(Long id) {
        return materialUnitDataMapper.selectMaterialUnitDataById(id);
    }

    /**
     * 查询物料单位列表
     *
     * @param materialUnitData 物料单位
     * @return 物料单位
     */
    @Override
    public List<MaterialUnitData> selectMaterialUnitDataList(MaterialUnitData materialUnitData) {
        return materialUnitDataMapper.selectMaterialUnitDataList(materialUnitData);
    }

    /**
     * 新增物料单位
     *
     * @param materialUnitData 物料单位
     * @return 结果
     */
    @Override
    public int insertMaterialUnitData(MaterialUnitData materialUnitData) {
                materialUnitData.setCreateTime(DateUtils.getNowDate());
            return materialUnitDataMapper.insertMaterialUnitData(materialUnitData);
    }

    /**
     * 修改物料单位
     *
     * @param materialUnitData 物料单位
     * @return 结果
     */
    @Override
    public int updateMaterialUnitData(MaterialUnitData materialUnitData) {
                materialUnitData.setUpdateTime(DateUtils.getNowDate());
        return materialUnitDataMapper.updateMaterialUnitData(materialUnitData);
    }

    /**
     * 批量删除物料单位
     *
     * @param ids 需要删除的物料单位主键
     * @return 结果
     */
    @Override
    public int deleteMaterialUnitDataByIds(Long[] ids) {
        int i = materialUnitDataMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除物料单位信息
     *
     * @param id 物料单位主键
     * @return 结果
     */
    @Override
    public int deleteMaterialUnitDataById(Long id) {
        return materialUnitDataMapper.deleteMaterialUnitDataById(id);
    }

    @Override
    public String getByIdAndCache(Long unit, HashMap<Long, String> unitNameCache) {
        return CacheSupportUtils.getFromCacheOrDB(unit, unitNameCache,this::selectMaterialUnitDataById, MaterialUnitData::getMaterialUnitName,false);
    }
}